const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        currentTab:0,
        dotation:false,
        use:false,
        withStatus:1,
      
        navData:[
            {
                text: '未赠送',
                status:-1
            },
            {
                text: '已赠送',
                status:0
            },
            {
                text: '已使用',
                status:1
            },
            {
                text: '已核销',
                status:2
            },
            {
                text: '已退款',
                status:7
            },
           
           
        ],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that=this;
        var status=options.status;
        if(status==undefined){
            status=-1;
        }
        this.setData({
            currentTab:status 
        })
        var userInfo=wx.getStorageSync('userInfo');
        wx.showShareMenu({
            menus: ['shareAppMessage', 'shareTimeline'],
            success(res) {
              console.log(res)
            },
            fail(e) {
              console.log(e)
            }
          })             
        that.setData({
            platform:app.globalData.platform,    
            userInfo:userInfo,
            width:app.globalData.windowWidth,
            height:app.globalData.windowHeight,
            statusBarHeight:app.globalData.statusBarHeight,
            navHeight:app.globalData.navHeight,
            marginTop:app.globalData.navTitleTop,
            domain:app.globalData.domain,
            uploadUrl:app.globalData.uploadUrl,
          })
    },
    switchNav(event){
        var cur = event.currentTarget.dataset.current;
        console.log(cur);  
        //每个tab选项宽度占1/5
        var singleNavWidth = this.data.width / 5;
        //tab选项居中                            
        this.setData({
            navScrollLeft: (cur - 2) * singleNavWidth
        })      
        if (this.data.currentTab == cur) {
            return false;
        } else {
            this.setData({
                currentTab: cur
            })
            this.getList();
          
        }
    },
    switchTab(event){
        var cur = event.detail.current;
        var singleNavWidth = this.data.width / 5;
        var left = (cur - 2) * singleNavWidth;
        this.setData({
            currentTab: cur,
            navScrollLeft: left
        });
    },
    goBack:function(){
        wx.navigateBack();
      },
    useCoupon:function(e){
        let that=this;
        var userInfo=wx.getStorageSync('userInfo');
        var coupon_no=e.currentTarget.dataset.coupon_no;
        wx.request({        
        url: app.globalData.domain+'coupon/getCity',
        data:{from:1,user_id:userInfo.id},
        success:res=>{
            console.log(res);
            if(res.statusCode==200){
                let data=res.data;
                //var city = Object.values(data.city); 
                var city=[];
                var citys=data.city;
                citys.forEach(function(item,index){
                    city[item.id]=item.city;
                });
                that.setData({
                    citys:citys,
                    city:city,
                    coupon_no:coupon_no,
                    use:true
                })
            }else{
               wx.showLoading({
                 title: '系统错误',
               }) 
            }
        }
        })
    
    },
    cityChange:function(e){
        //console.log(e)
        let that=this;
        var index=e.detail.value;
        var cityIndex=this.data.cityIndex;
        var citys=this.data.citys;
        var userInfo=wx.getStorageSync('userInfo');
        var city=citys[index].id;
        if(index>0){
            if(index!=cityIndex){
               // this.getArea()
               wx.request({
                url: app.globalData.domain+'coupon/getArea',
                data:{from:1,user_id:userInfo.id,city:city},
                success:res=>{
                    console.log(res);
                    if(res.statusCode==200){
                        let data=res.data;                
                        var area=[];
                        var areas=data.area;
                        console.log(areas);
                        areas.forEach(function(item,index){
                            console.log(item);
                            area[index]=item.city;
                        });
                        console.log(area);
                        that.setData({
                            areas:areas,
                            area:area,               
                       
                        })
                    }else{
                       wx.showLoading({
                         title: '系统错误',
                       }) 
                    }
                }
               })
            }
        }
        this.setData({
            cityIndex:index,
            areaIndex:0
        })
    },
    areaChange:function(e){
        //console.log(e)
        let that=this;
        var index=e.detail.value;
        var areaIndex=this.data.areaIndex;
        var areas=this.data.areas;
        var userInfo=wx.getStorageSync('userInfo');
        var area=areas[index].id;
        if(index>0){
            if(index!=areaIndex){
               // this.getArea()
               wx.request({
                url: app.globalData.domain+'coupon/getStore',
                data:{from:1,user_id:userInfo.id,area:area},
                success:res=>{
                    console.log(res);
                    if(res.statusCode==200){
                        let data=res.data; 
                       console.log(data);
                        var store=[];
                        var stores=data.store;
                        console.log(stores);
                        stores.forEach(function(item,index){
                            console.log(item);
                            store[index]=item.name;
                        });
                        console.log(area);
                        that.setData({
                            stores:stores,
                            store:store,               
                       
                        })
                    }else{
                       wx.showLoading({
                         title: '系统错误',
                       }) 
                    }
                }
               })
            }
        }
        this.setData({
            areaIndex:index,
            storeIndex:0
        })
    },
    storeChange:function(e){
        let that=this;
        var index=e.detail.value;
        var storeIndex=this.data.storeIndex;
        var stores=this.data.stores;
        var userInfo=wx.getStorageSync('userInfo');
        var store=stores[index].id;
        if(index>0){
            if(index!=storeIndex){
               var  model=stores[index].model;
               var arr=model.split(",");
                console.log(arr);
                this.setData({
                    model:arr
                })
            }
        }
        this.setData({
            storeIndex:index,
            modelIndex:0
        })
    },
    modelChange:function(e){
        let that=this;
        var index=e.detail.value;
        var modelIndex=this.data.modelIndex;
        var model=this.data.model;
        var models= model[idnex];      
            this.setData({
                modelIndex:index,
                models:models
            })
        
    },
    inputUserPhone:function(e){
        console.log(e);
        var value=e.detail.value;
        if(value){
            console.log(value);
            this.setData({
                userphone:value
            })
        }else{
            this.setData({
                userphone:undefined
            }) 
        }
       
    },
    inputUserName:function(e){
        console.log(e);
        var value=e.detail.value;
        if(value){
            console.log(value);
            this.setData({
                username:value
            })
         }else{
            this.setData({
                username:undefined
            }) 
        }
    
    },
    inputVin:function(e){
        console.log(e);
        var value=e.detail.value;
        if(value){
            console.log(value);
        this.setData({
            vin:value
        })
        }else{
            this.setData({
                vin:undefined
            }) 
        }
    
    },
    showModals:function(msg){
        wx.showModal({
            title: '系统提示',
            content:msg,
            success:function(res){
                return false;
            }
        })
    },
    toUse:function(e){
        let that = this;
        var userInfo = wx.getStorageSync('userInfo');
        let data=this.data;
        console.log(data);
        if(data.username==undefined){            
            this.showModals('请输入姓名');
        }else if(data.userphone==undefined){
            this.showModals('请输入电话');
        }else if(!(/^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(data.userphone))){
          this.showModals('请输入正确的电话号码');
        }else if(data.cityIndex==0){
            this.showModals('请选择城市');
        }else if(data.areaIndex==0){
            this.showModals('请选择县区');
        }else if(data.sotreIndex==0){
            this.showModals('请选择网点');
        }else if(!(/[A-Za-z0-9]{8}$/.test(data.vin))){
            this.showModals('请输入车架号');  
        }else{
            var store=data.stores;
            var storeIndex=data.storeIndex;
            var store_id=store[storeIndex].id;
            var model = data.model;
            var modelIndex = data.modelIndex;
            var models = model[modelIndex];
            wx.request({
                url: app.globalData.domain+'coupon/useCoupon',
                data:{from:1,user_id:userInfo.id,
                    username:data.username,
                    userphone:data.userphone,
                    store:store_id,
                    model:models,
                    vin:data.vin,
                    coupon_no:data.coupon_no},
                success:res=>{
                    console.log(res);
                    var data=res.data;
                    if(data.status==200){
                        wx.showModal({
                            title: '系统提示',
                            content:data.message,
                            success:function(res){
                                that.useClose();
                                that.getList();
                                that.setData({
                                    use:false
                                })
                            }
                        })
                    }else{
                        wx.showModal({
                            title: '系统提示',
                            content:data.message,
                            success:function(res){
                                return false;
                            }
                        })
                    }
                }
            })
        }
    },
    useClose:function(){
        this.setData({
            coupon_no:'',
            username:undefined,
            userphone:undefined,
            vin:undefined,
            cityIndex:0,
            areaIndex:0,
            storeIndex:0,            
            use:false
        }) 
    },
    dotation:function(e){
       var coupon_no=e.currentTarget.dataset.coupon_no;
        this.setData({
            coupon_no:coupon_no,
            dotation:true
        })
    },
    closeDotation:function(){
        this.setData({
            coupon_no:'',
            dotation:false
        }) 
    },
    toDotation:function(e){
       let that=this;
       let data=this.data;
       var phone=data.phone;
       var userInfo = wx.getStorageSync('userInfo');
       var coupon_no = data.coupon_no;
        if (!(/^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(phone))) {
            wx.showToast({
              title: '手机号输入错误',
              icon: 'none'
            })
            return;
          }else if(coupon_no==""){
            wx.showToast({
                title: '券号错误',
                icon: 'none'
              })
        }else{
            wx.request({
                url: app.globalData.domain+'coupon/dotation',
                data:{from:1,user_id:userInfo.id,coupon_no:coupon_no,phone:phone},
                success:res=>{
                  var data=res.data;
                  console.log(data);
                if(data.status==200){
                    that.setData({                     
                        dotation:false
                      })
                    wx.showModal({
                        title:'提示信息',
                        content:data.message,
                        success:function(e){
                            if(e.confirm){
                                that.getList();
                            }
                        }
                      })
               
                }else{
                  wx.showModal({
                    title:'提示信息',
                    content:data.message,
                    success:function(){

                    }
                  })
                }
                
                }
            })
        }
    },
    inputPhone:function(e){
        console.log(e);
        var phone=e.detail.value;
        this.setData({
            phone:phone
        })
    },
    gotoWith(e){
        console.log(e);
        var userInfo = wx.getStorageSync('userInfo');
        var coupon_no = e.currentTarget.dataset.coupon_no;
        var id = e.currentTarget.dataset.id
        let withStatus = this.data.withStatus;
        let that = this;
        wx.showModal({
            title: '系统提示',
            content: '您确定要提现么？',
            confirmText:'确定提现',
            cancelText:'考虑一下',
            complete: (res) => {
              if (res.cancel) {
                
              }
          if (res.confirm) {
              if(withStatus==1){
  
             
                  this.setData({
                      withStatus:0
                  })
                  setTimeout(function () {
                   that.setData({
                       withStatus: 1
                   })
                 }, 2000) //阻止一秒点击多次
                  wx.request({
                      url: app.globalData.domain+'share/gotoWith',
                      data:{from:1,user_id:userInfo.id,id:id,coupon_no:coupon_no},
                      success:res=>{    
                         // console.log(res);
                         var data=res.data;
                         if(data.code=200){
                              that.showModals(data.desc)
                              //that.Index();
                         }else{
                              that.showModals(data.desc)
                             // that.Index();
                         }
                      }
                  });
              }else{
                  wx.showToast({
                      title: '请请勿重新点击',
                    })
              }
              }
            }
          })
    },
    refund(e){
        console.log(e);
        var id=e.currentTarget.dataset.id
        var coupon_no=e.currentTarget.dataset.coupon_no;
        var userInfo=wx.getStorageSync('userInfo');
        var page=this.data.page;
        let that=this;
        let withStatus = this.data.withStatus;
        wx.showModal({
          title: '系统提示',
          content: '您确定要退款么？',
          cancelText:'考虑一下',
          confirmText:'立即退款',
          complete: (res) => {
            if (res.cancel) {
              
            }
        
            if (res.confirm) {
                if(withStatus==1){

           
                    this.setData({
                        withStatus:0
                    })
                    setTimeout(function () {
                     that.setData({
                         withStatus: 1
                     })
                   }, 2000) //阻止一秒点击多次
                wx.request({
                    url: app.globalData.domain+'pay/refund',
                    data:{from:1,user_id:userInfo.id,id:id,coupon_no:coupon_no},
                    success:res=>{    
                       console.log(res);
                       var data=res.data;
                       if(data.code ==200){
                            that.showModals(data.desc)
                            that.getList()
                       }else{
                            that.showModals(data.desc)
                            that.getList()
                       }
                    }
                });}else{
                    wx.showToast({
                        title: '请请勿重新点击',
                      })
                 }
            }
          }
        })
 

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        
        this.getList();
    },
  getList(){
      let that=this;
      let data=this.data;
      var userInfo=wx.getStorageSync('userInfo');
      var currentTab=data.currentTab;
      var navData=data.navData;
      console.log('currentTab'+currentTab);
      var status=navData[currentTab].status;
      var user_id=userInfo.id;
      wx.request({
        url: app.globalData.domain+'coupon/getList',
        data:{from:1,user_id:user_id,status:status},
        success:res=>{
          var data=res.data;
          console.log(data);
          that.setData({
            coupon:data.coupon,
            count:data.count
          })
        }
    })
  },
  goto:function(e){
    console.log(e);
    var url=e.currentTarget.dataset.url;
    var index = e.currentTarget.dataset.index;
    var parent_id=wx.getStorageSync('parent_id');
    if(url=="#"){

    }else if(url=='use'){
        var coupon_no=e.currentTarget.dataset.coupon_no;
        var id=e.currentTarget.dataset.id;
        wx.navigateTo({
          url: 'use?coupon_no='+coupon_no+'&id='+id,
        })
    }else if(url=='phone'){
        wx.makePhoneCall({
          phoneNumber: '400-800-2177',
        })     
    }else{
       app.goto(url,index)
    }
    console.log(url);
},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})